package com.itheima.service.system.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.system.Dept;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.system.DeptMapper;
import com.itheima.service.system.DeptService;
import com.itheima.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 部门 业务层 实现
 */
public class DeptServiceImpl implements DeptService {
    @Override
    public Page<Dept> findAll(String pageNum, String pageSize) {
        //获取sqlSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        DeptMapper mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
        //配置分页信息
        int num = 1;
        int size = 10;
        //阿帕奇的StringUtils.isNotBlank() 可以同时判断空, 无值, 空格
        if (StringUtils.isNotBlank(pageNum)) {
            num = Integer.parseInt(pageNum);
        }
        if (StringUtils.isNotBlank(pageSize)) {
            size = Integer.parseInt(pageSize);
        }
        Page<Dept> page = PageHelper.startPage(num, size);
        //查找全部
        mapper.findAll();
        //释放资源
        TransactionUtil.close(sqlSession);

        return page;
    }

    @Override
    public List<Dept> findAll() {
        //获取sqlSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        DeptMapper mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
        //查找全部
        Page<Dept> list = mapper.findAll();
        //释放资源
        TransactionUtil.close(sqlSession);

        return list;
    }

    @Override
    public void addDept(Dept dept) {
        //获取sqlSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        DeptMapper mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
        //查找全部
        mapper.addDept(dept);
        //提交事务
        TransactionUtil.commit(sqlSession);
        //释放资源
        TransactionUtil.close(sqlSession);

    }

    @Override
    public boolean deleteByIds(String ids) {
        try {
            SqlSession sqlSession = MapperFactory.getSqlSession();
            //获取映射对象
            DeptMapper mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
            //ids处理
            String[] idArr = ids.split(",");
            mapper.deleteByIds(idArr);
            //提交事务
            TransactionUtil.commit(sqlSession);
            //释放资源
            TransactionUtil.close(sqlSession);

        } catch (Exception e) {
            return false;
        }

        return true;
    }

    @Override
    public Dept findById(String id) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        DeptMapper mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
        //查找用户
        Dept dept = mapper.findById(id);
        //释放资源
        TransactionUtil.close(sqlSession);
        return dept;
    }

    @Override
    public void updateDept(Dept dept) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        DeptMapper mapper = MapperFactory.getMapper(sqlSession, DeptMapper.class);
        //更新信息
        mapper.updateDept(dept);
        //提交事务
        TransactionUtil.commit(sqlSession);
        //释放资源
        TransactionUtil.close(sqlSession);
    }
}
